package com.xwj.nio.netty.echoserver;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author xwj
 * @date 2020/6/15
 */
public class NettyEchoClientHandler extends ChannelInboundHandlerAdapter {
    public static final NettyEchoClientHandler INSTANCE = new NettyEchoClientHandler();

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf) msg;
        int len = buf.readableBytes();
        byte[] arr = new byte[len];
        buf.getBytes(0, arr);
        System.out.println("客户端回显：" + new String(arr, "UTF-8"));

        // 1、手动释放ByteBuf
        buf.release();

        // 2、调用父类的入站方法，向后传递，交由尾部的handler处理器进行释放
//        super.channelRead(ctx, msg);
    }
}
